package com.fingard.dsp.bank.directbank.hzcb01;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class HZCB01BatPayRec extends HZCB01Base implements IBatPayRec {
    //1988
    public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception {
        return d_BatPayRec(p_req1988);
    }

    //9188
    @Override
    public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
        return null;
    }

    //代发工资提交
    public RetAtsBatPayRec d_BatPayRec(ReqBatPayRec p_reqBat) throws Exception {

        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
        try {
            txCode = "PY4005";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            writeCommonHeader(xmlWriter);
            xmlWriter.writeStartElement("input");
            xmlWriter.writeElementString("compFlowNo", p_reqBat.reqHeader.reqSeqID);
            xmlWriter.writeElementString("payAccount", p_reqBat.ownAct);
            xmlWriter.writeElementString("payAccountName", p_reqBat.ownActName);
            xmlWriter.writeElementString("bsnType", "01");
            xmlWriter.writeElementString("protocolNo", getBankConfig().getValueAsString("protocolNo"));//todo  未知 必填项
            xmlWriter.writeElementString("currencyType", convertCurToBank(p_reqBat.ownCur));
            xmlWriter.writeElementString("payUse", "批量代发");
            xmlWriter.writeElementString("totalAmt", p_reqBat.totalAmt);
            xmlWriter.writeElementString("totalNum", p_reqBat.totalNum);
            int count = 0;
            xmlWriter.writeStartElement("result");
            while (p_reqBat.readRecord()) {
                count++;
                xmlWriter.writeStartElement("row");
                xmlWriter.writeElementString("workNo", p_reqBat.readedDetail.abstractATS);
                xmlWriter.writeElementString("recAccount", p_reqBat.readedDetail.recAct);
                xmlWriter.writeElementString("recAccountName", p_reqBat.readedDetail.recName);
                xmlWriter.writeElementString("payAmount", p_reqBat.readedDetail.payAmount);
                xmlWriter.writeElementString("comment", p_reqBat.readedDetail.purpose);
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeEndElement();
            xmlWriter.writeEndElement();
            xmlWriter.writeEndElement();
            String signXml = getDataSignData(xmlWriter);
            if (signnatrue(signXml) == true) {
                xmlWriter.writeElementString("signData", signature);
                xmlWriter.writeEndDocument();//写入数据完成
                String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));//发送银行
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpStatus = tmpBkReader.getFirstText("/HZBANKB2C/ebank/common/retCode");
                        String tmpInfo = tmpBkReader.getFirstText("/HZBANKB2C/ebank/common/retMsg");
                        if(isActiveRetCode(tmpStatus)){
                            //50：主机处理中 90：交易成功；91：交易失败；99：交易可疑
                            String orderSTT = tmpBkReader.getFirstText("/HZBANKB2C/ebank/output/orderSTT");
                            if(tmpBkReader.read("/HZBANKB2C/ebank/output/result/row")){
                                retAts.respCode = "0";
                                retAts.reqSeqID = p_reqBat.reqBatchNO;
                                retAts.respInfo = tmpBkReader.readedXElement.getSingleText("errorMessage");
                                //0：检查成功 （当 orderSTT 状态为 50的时候，此处0代表“等待处理”）1：检查失败
                                String orderState = tmpBkReader.readedXElement.getSingleText("orderState");
                                retAts.transState = orderSTT;
                                retAts.payInfoCode = retAts.transState;
                            }else{
                                retAts.respCode = "0";
                                retAts.transState = tmpStatus;
                                retAts.payInfoCode = tmpStatus;
                                retAts.respInfo = tmpInfo;
                                retAts.reqSeqID =  p_reqBat.reqBatchNO;
                            }
                        }else{
                            retAts.respCode = "-1";
                            retAts.transState = tmpStatus;
                            retAts.respInfo = tmpInfo;
                        }
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpSbMsg.toString();
                    }
                } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                    retAts.respCode = "0";
                    retAts.respInfo = tmpStrRet[1];
                    retAts.reqSeqID = p_reqBat.reqBatchNO;
                    retAts.transState = PaymentState.FAIL_Batch;
                    retAts.payInfoCode = tmpStrRet[0];
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpStrRet[1];
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpSign[1];
            }
        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = e.toString();
            e.printStackTrace();
        }
        if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
            retAts.reqSeqID = p_reqBat.reqBatchNO;
        }
        retAts.writeRetToATS();
        return retAts;
    }

    //1998 状态查询
    public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
        return d_QryBatPayRec(p_req1998);
    }

    //9198
    @Override
    public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
        return null;
    }

    //批量代付和工资代付状态查询
    public RetAtsQryBatPayRec d_QryBatPayRec(ReqQryBatPayRec p_reqQryBat) throws Exception {
        //返回ATS的对象
        RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_reqQryBat.reqHeader);
        txCode = "PY4009";
        XmlTextWriter xmlWriter;
        xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        writeCommonHeader(xmlWriter);//写入通用的头部节点
        xmlWriter.writeStartElement("input");
        xmlWriter.writeElementString("compFlowNo", p_reqQryBat.reqHeader.reqSeqID);
        xmlWriter.writeEndElement();
        xmlWriter.writeEndElement();
        String signXml = getDataSignData(xmlWriter);//获取签名数据
        try {
            if (signnatrue(signXml) == true) {
                xmlWriter.writeElementString("signData", signature);//进行签名
                xmlWriter.writeEndDocument();//写入数据完成

                String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()));
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String retCode = tmpBkReader.getFirstText("/HZBANKB2C/ebank/common/retCode");
                        String tmpStatus = tmpBkReader.getFirstText("/HZBANKB2C/ebank/common/retMsg");
                        if (tmpStatus != null && tmpStatus.length() > 0) {
                            retAts.respCode = "0";
                            retAts.reqSeqID = p_reqQryBat.reqBatchNO;
                            int tmpCountRow = tmpBkReader.countElement("/HZBANKB2C/ebank/output/turnPageTotalNum");
                            if (tmpCountRow > 0 && isActiveRetCode(retCode)) {
                                retAts.writeBeforeDetail();
                                //String state = tmpBkReader.getFirstText("/HZBANKB2C/ebank/output/orderSTT");
                                while (tmpBkReader.read("/HZBANKB2C/ebank/output/result/row")) {
                                    RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
                                    String orderState = tmpBkReader.readedXElement.getSingleText("orderState");
                                    tmpRetDetail.recAct = tmpBkReader.readedXElement.getSingleText("recAccount");
                                    tmpRetDetail.recName = tmpBkReader.readedXElement.getSingleText("recAccountName");
                                    tmpRetDetail.payAmount = tmpBkReader.readedXElement.getSingleText("payAmount");
                                    tmpRetDetail.result = orderState;
                                    tmpRetDetail.postScript = tmpBkReader.readedXElement.getSingleText("workNo");
                                    tmpRetDetail.usage = tmpBkReader.readedXElement.getSingleText("comment");
                                    tmpRetDetail.payInfoCode = orderState;
                                    tmpRetDetail.memo = tmpBkReader.readedXElement.getSingleText("errorMessage");
                                    retAts.writeRetDetail(tmpRetDetail);
                                }
                            } else if (tmpCountRow == 0) {
                                retAts.respCode = "-1";
                                retAts.respInfo = "状态查询结果为空";
                            } else {
                                retAts.respCode = "-1";
                                retAts.respInfo = tmpStatus;
                            }
                        } else {
                            retAts.respCode = "-1";
                            retAts.respInfo = tmpStatus;
                        }
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpStrRet[1];
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpSign[1];
            }
        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = e.getMessage();
            e.printStackTrace();
        }
        if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
            retAts.reqSeqID = p_reqQryBat.reqBatchNO;
        }
        if (retAts.respCode.equals("-1")) {
            retAts.writeBeforeDetail();
        }
        retAts.writeAfterDetail();
        return retAts;
    }
}
